<?php

/**
 * @file
 * Builds placeholder replacement tokens for user-related data.
 */

/**
 * Implements hook_token_info().
 */
function chatroom_livesupport_token_info() {
  $types['chatroom'] = array(
    'name' => t('Chatroom'),
    'description' => t('Tokens related to chatroom.'),
    'needs-data' => 'chatroom',
  );
  $types['chatroom-visitor'] = array(
    'name' => t('Chatroom Visitor'),
    'description' => t('Tokens related to the user.'),
    'type' => 'user',
  );
  $types['chatroom-operator'] = array(
    'name' => t('Chatroom Operator'),
    'description' => t('Tokens related to the user.'),
    'type' => 'user',
  );
  $chatroom['cid'] = array(
    'name' => t('Chatroom ID'),
    'description' => t("The unique ID of the chatroom."),
  );
  $chatroom['status'] = array(
    'name' => t("Status"),
    'description' => t("The status of the chatroom."),
  );
  $chatroom['modified'] = array(
    'name' => t("Modified"),
    'description' => t("The modified of the chatroom."),
    'type' => 'date',
  );
  $chatroom_visitor['modified'] = array(
    'name' => t("Modified"),
    'description' => t("The access date visitor of the chatroom."),
    'type' => 'date'
  );
  $chatroom_operator['modified'] = array(
    'name' => t("Modified"),
    'description' => t("The access date operator of the chatroom."),
    'type' => 'date'
  );
  return array(
    'types' => $types,
    'tokens' => array('chatroom' => $chatroom, 'chatroom-operator' => $chatroom_operator),
  );
}

/**
 * Implements hook_tokens().
 */
function chatroom_livesupport_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $url_options = array('absolute' => TRUE);
  if (isset($options['language'])) {
    $url_options['language'] = $options['language'];
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $sanitize = !empty($options['sanitize']);

  $replacements = array();

  if ($type == 'chatroom' && !empty($data['chatroom'])) {
    $chatroom = $data['chatroom'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        //Basic chatroom information
        case 'cid':
          $replacements[$original] = !empty($chatroom->cid) ? $chatroom->cid : t('not yet assigned');
          break;
        case 'status':
          $states = chatroom_livesupport_chatroom_states();
          $replacements[$original] = !empty($chatroom->status) ? $states[$chatroom->status]  : t('not yet assigned');
          break;
        case 'modified':
          $replacements[$original] = !empty($chatroom->modified) ? format_date($chatroom->modified, 'medium', '', NULL, $language_code) : t('not yet created');
          break;
      }
    }

    if ($registered_tokens = token_find_with_prefix($tokens, 'modified')) {
      $replacements += token_generate('date', $registered_tokens, array('date' => $chatroom->modified), $options);
    }
  }

  if ($type == 'chatroom-visitor' && !empty($data['chatroom'])) {
    $chatroom =  $data['chatroom'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        //Basic chatroom operator information
        case 'modified':
          $visitor = db_query("SELECT modified FROM {chatroom_user} WHERE uid = {$chatroom->uid} AND cid = {$chatroom->cid}")->fetchAssoc();
          $replacements[$original] = !empty($visitor['modified']) ? format_date($operator['modified'], 'medium', '', NULL, $language_code) : t('not yet modified');
          break;
      }
    }
    $account = user_load($chatroom->uid);
    $replacements += token_generate('user', $tokens, array('user' => $account), $options);
  }

  if ($type == 'chatroom-operator' && !empty($data['chatroom'])) {
    $chatroom =  $data['chatroom'];
    if (!empty($data['operator'])) {
      $operator = $data['operator'];
    }
    else {
      $operator = db_query("SELECT u.uid, u.name, cu.modified FROM {chatroom_user} cu
        INNER JOIN {chatroom_operator} co ON cu.uid = co.uid
        LEFT JOIN {users} u ON u.uid = cu.uid
        WHERE cu.cid = {$chatroom->cid} ORDER BY modified DESC LIMIT 1")->fetchAssoc();
      $operator = (object)$operator;
    }
    if ($operator) {
      foreach ($tokens as $name => $original) {
        switch ($name) {
          //Basic chatroom operator information
          case 'modified':
            $replacements[$original] = !empty($operator->modified) ? format_date($operator->modified, 'medium', '', NULL, $language_code) : t('not yet modified');
            break;
        }
      }
      $account = user_load($operator->uid);
      $replacements += token_generate('user', $tokens, array('user' => $account), $options);
    }
  }
  return $replacements;
}
